<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\MessageConfig;
use app\admin\model\PlatformUpload;
use app\admin\model\SystemUser;
use app\admin\model\SystemUserRole;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Platform extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\Platform();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where = [];
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }

        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['telphone','=',$param['telphone']];
        }

        $count = $this->model
            ->where($where)
            ->count();
        $list = $this->model
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['expiration_time']  = date('Y-m-d H:i',$value['expiration_time']);

        }
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }


    public function add()
    {
        $request = $this->request;
        $param = $request->param();
        $sysconfig  = sysconfig('site');
        $data = [
            'telphone' => $param['telphone'],
            'name' => $param['name'],
            'status' => $param['status'],
            'url' =>  $param['url'],
            'expiration_time' => strtotime($param['expiration_time']),
            'application_rate' => isset($param['application_rate']) &&  $param['application_rate'] ? $param['application_rate'] :$sysconfig['application_rate'],
            'mch_no' => $param['mch_no'],
            'app_id' => $param['app_id'],
            'private_key' => $param['private_key'],
            'share_banner' => $param['url'].'/imgs/share_banner.jpg',
            'class_img' =>  $param['url']. '/imgs/class.png',


        ];

        $this->model->save($data);
        $systemUserData  = [
            'username' => $param['telphone'],
            'password' => md5($param['password']),
            'nickname' =>  $param['telphone'],
            'phone' => $param['telphone'],
            'tenant_id' => 2,
            'platform_id' =>   $this->model->id,
            'avatar' =>   '/static/admin.png',
            'role_id' => 2,
        ];


        $systemUserModel = new SystemUser();
        $systemUserModel->save($systemUserData);
        $systemUserRole = new SystemUserRole();
        $systemUserRole->save(['user_id' =>$systemUserModel->id ,
            'role_id'=> 2,  'tenant_id' => 2]);

        return msg([], 0, "添加成功!");
    }

    public function edit()
    {
        $request = $this->request;
        $param = $request->param();
        $platformInfo =  $this->model->where(['id'=>$param['id'] ])->find();
        //var_dump(strtotime($param['expiration_time']));exit;
        $data = [
            'telphone' => $param['telphone'],
            'name' => $param['name'],
            'status' => $param['status'],
            'url' =>  $param['url'],
            'expiration_time' => strtotime($param['expiration_time']),
            'mch_no' => $param['mch_no'],
            'app_id' => $param['app_id'],
            'private_key' => $param['private_key'],
            'application_rate' => isset($param['application_rate']) &&  $param['application_rate'] ? $param['application_rate'] :0,

        ];
        $this->model->where(['id' => $param['id']])->update($data);

        $systemUserData  = [
            'username' => $param['telphone'],
            'password' => isset($param['password']) && $param['password']? md5($param['password']): $platformInfo['password'],
            'nickname' =>  $param['telphone'],
            'phone' => $param['telphone'],
            'tenant_id' => 2,
            'avatar' =>   '/static/admin.png',


        ];
        $systemUserModel = new SystemUser();
        $systemUserModel->where(['platform_id' => $platformInfo['platform_id'], 'role_id' => 2])->update($systemUserData);
        return msg([], 0, "添加成功!");
    }

    public function query(){

        $where = [];
        if ($this->request->platform_id >0 ){
            $where[] = ['id','=',$this->request->platform_id];
        }
        $data = $this->model
            ->where($where)->find();
        return msg($data, 0, "查询成功");
    }

    public function save(){

        $where = [];
        if ($this->request->platform_id >0 ){
            $where[] = ['id','=',$this->request->platform_id];
        }
        $request = $this->request;
        $param = $request->param();
        unset($param['create_time']);
        unset($param['delete_time']);
        unset($param['update_time']);
        $this->model->where($where)->update($param);
        return msg([], 0, "保存成功");

    }

    public function uploadConfigQuery(){
        $where = [
            'platform_id' => $this->request->platform_id,
        ];
        $platformUploadModel = new PlatformUpload();
        $data =  $platformUploadModel->where($where)->find();
        return msg($data, 0, "查询成功");
    }
    public function uploadConfigSave(){
        $request = $this->request;
        $param = $request->param();

        $where['platform_id'] = $this->request->platform_id;
        $platformUploadModel = new PlatformUpload();
        $platformUploadInfo =  $platformUploadModel->where($where)->find();
        if ($platformUploadInfo){
            $platformUploadInfo->save($param);
        }else{
            $param['platform_id'] = $this->request->platform_id;;
            $platformUploadModel->save($param);
        }

        return msg([], 0, "保存成功");
    }


    public function messageConfigQuery(){
        $where = [
            'platform_id' => $this->request->platform_id,
        ];
        $platformUploadModel = new MessageConfig();
        $data =  $platformUploadModel->where($where)->find();
        return msg($data, 0, "查询成功");
    }
    public function messageConfigSave(){
        $request = $this->request;
        $param = $request->param();

        $where['platform_id'] = $this->request->platform_id;
        $platformUploadModel = new MessageConfig();
        $platformUploadInfo =  $platformUploadModel->where($where)->find();
        if ($platformUploadInfo){
            $platformUploadInfo->save($param);
        }else{
            $param['platform_id'] = $this->request->platform_id;;
            $platformUploadModel->save($param);
        }

        return msg([], 0, "保存成功");
    }

    public function resetPassword(){
        $request = $this->request;
        $param = $request->param();
        $systemUserData  = [
            'password' => md5(123456),
        ];
        $systemUserModel = new SystemUser();
        $systemUserModel->where([    'platform_id' =>   $param['id'],])->update($systemUserData);
        return msg([], 0, "保存成功");
    }
}